package com.ysd.lis.service.sta.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaPurposeAndDetailDto;
import com.ysd.lis.dto.sta.StaPurposeDto;
import com.ysd.lis.entity.sta.StaReqPurpose;
import com.ysd.lis.entity.sta.StaReqPurposeDetail;
import com.ysd.lis.entity.sta.StaSampleType;
import com.ysd.lis.entity.sta.StaWorkFlow;
import com.ysd.lis.mapper.sta.StaReqPurposeDetailMapper;
import com.ysd.lis.request.StaPurposeParam;
import com.ysd.lis.service.sta.StaReqPurposeDetailService;
import com.ysd.lis.service.sta.StaReqPurposeService;
import com.ysd.lis.service.sta.StaWorkFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-04-11
 */
@Service
public class StaReqPurposeDetailServiceImpl extends ServiceImpl<StaReqPurposeDetailMapper, StaReqPurposeDetail> implements StaReqPurposeDetailService {
    @Autowired
    StaReqPurposeDetailMapper bacReqPurposeDetailMapper;
    @Autowired
    StaReqPurposeService bacReqPurposeService;
    //@Autowired
    // LabSampleTypeService labSampleTypeService;
    @Autowired
    StaWorkFlowService bacWorkFlowService;

    @Override
    public Result findPurposeDetailList(StaPurposeParam param) {
        MPJLambdaWrapper<StaReqPurposeDetail> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(StaReqPurposeDetail.class);
        queryWrapper.selectAs("sa", StaSampleType::getSampleTypeName, StaReqPurposeDetail::getSampleTypeName);
        queryWrapper.selectAs("wo", StaWorkFlow::getWorkFlowName, StaReqPurposeDetail::getSxWorkFlowName);
        queryWrapper.selectAs("bwo", StaWorkFlow::getWorkFlowName, StaReqPurposeDetail::getBxWorkFlowName);
        queryWrapper.leftJoin(StaSampleType.class, "sa", p -> p.eq(StaSampleType::getSampleTypeCode, StaReqPurposeDetail::getSampleTypeCode).eq(StaSampleType::getDelFlag, 0));
        queryWrapper.leftJoin(StaWorkFlow.class, "wo", p -> p.eq(StaWorkFlow::getWorkFlowCode, StaReqPurposeDetail::getSxWorkFlowNo).eq(StaWorkFlow::getDelFlag, 0));
        queryWrapper.leftJoin(StaWorkFlow.class, "bwo", p -> p.eq(StaWorkFlow::getWorkFlowCode, StaReqPurposeDetail::getBxWorkFlowNo).eq(StaWorkFlow::getDelFlag, 0));
        queryWrapper.eq(StaReqPurposeDetail::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getPurposeNo())) {
            queryWrapper.and(p -> p.eq(StaReqPurposeDetail::getPurposeNo, param.getPurposeNo()).eq(StaReqPurposeDetail::getDelFlag, 0));
        }
        queryWrapper.orderByAsc(StaReqPurposeDetail::getSeq);
        List<StaReqPurposeDetail> detailList = bacReqPurposeDetailMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", detailList);
    }

    @Override
    public Result findPurposeAndDetailList(StaPurposeParam param) {
        List<StaPurposeAndDetailDto> returnList = new ArrayList<>();
        String purposeId = param.getPurposeId();
        StaReqPurpose bacReqPurpose = bacReqPurposeService.getById(purposeId);
        StaPurposeAndDetailDto purpose = new StaPurposeAndDetailDto();
        purpose.setId(bacReqPurpose.getId());
        purpose.setSampleTypeId(bacReqPurpose.getSampleTypeCode());
        // if (StringUtils.isNotBlank(bacReqPurpose.getSampleTypeCode())) {
        //     StaSampleType labSampleType = labSampleTypeService.findOneByNo(bacReqPurpose.getSampleTypeCode());
        //     if (null != labSampleType) {
        //         purpose.setSampleTypeName(labSampleType.getSampleTypeName());
        //     }
        // }
        purpose.setBxWorkFlowNo(bacReqPurpose.getBxFlowNo());
        if (StringUtils.isNotBlank(bacReqPurpose.getBxFlowNo())) {
            StaWorkFlow bacWorkFlow = bacWorkFlowService.findOneByNo(bacReqPurpose.getBxFlowNo());
            purpose.setBxWorkFlowName(BeanUtil.isEmpty(bacWorkFlow) ? "" : bacWorkFlow.getWorkFlowName());
        }
        purpose.setBxCulCombinaNos(bacReqPurpose.getBxCulCombinaNos());
        purpose.setDataType("purpose");
        returnList.add(purpose);

        MPJLambdaWrapper<StaReqPurposeDetail> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(StaReqPurposeDetail.class);
        queryWrapper.selectAs("sa", StaSampleType::getSampleTypeName, StaReqPurposeDetail::getSampleTypeName);
        queryWrapper.selectAs("bwo", StaWorkFlow::getWorkFlowName, StaReqPurposeDetail::getBxWorkFlowName);
        queryWrapper.leftJoin(StaSampleType.class, "sa", p -> p.eq(StaSampleType::getSampleTypeCode, StaReqPurposeDetail::getSampleTypeCode).eq(StaSampleType::getDelFlag, 0));
        queryWrapper.leftJoin(StaWorkFlow.class, "bwo", p -> p.eq(StaWorkFlow::getWorkFlowCode, StaReqPurposeDetail::getBxWorkFlowNo).eq(StaWorkFlow::getDelFlag, 0));
        queryWrapper.eq(StaReqPurposeDetail::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getPurposeNo())) {
            queryWrapper.and(p -> p.eq(StaReqPurposeDetail::getPurposeNo, param.getPurposeNo()));
        }
        queryWrapper.orderByAsc(StaReqPurposeDetail::getSeq);
        List<StaReqPurposeDetail> detailList = bacReqPurposeDetailMapper.selectList(queryWrapper);

        for (StaReqPurposeDetail detail : detailList) {
            StaPurposeAndDetailDto pd = new StaPurposeAndDetailDto();
            pd.setId(detail.getId());
            pd.setSampleTypeId(detail.getSampleTypeCode());
            pd.setSampleTypeName(detail.getSampleTypeName());
            pd.setBxWorkFlowNo(detail.getBxWorkFlowNo());
            pd.setBxWorkFlowName(detail.getBxWorkFlowName());
            pd.setBxCulCombinaNos(detail.getBxCulCombinaNos());
            pd.setDataType("detail");

            returnList.add(pd);
        }

        return Result.succ(1, "查询成功", returnList);
    }

    @Override
    public Result deleteBatchPurposeDetail(StaPurposeDto dto) {
        List<StaReqPurposeDetail> detailList = dto.getDetailList();
        List<String> ids = detailList.stream().map(StaReqPurposeDetail::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result saveOrUpdatePurposeDetail(StaPurposeDto dto) {
        List<StaReqPurposeDetail> detailList = dto.getDetailList();
        boolean b = this.saveOrUpdateBatch(detailList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result updatePurposeAndDetail(StaPurposeDto dto) {
        List<StaPurposeAndDetailDto> purposeAnddetailList = dto.getPurposeAnddetailList();
        List<StaPurposeAndDetailDto> purposeList = purposeAnddetailList.stream().filter(p -> p.getDataType().equals("purpose")).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(purposeList)) {
            List<StaReqPurpose> upPurposeList = new ArrayList<>();
            for (StaPurposeAndDetailDto pur : purposeList) {
                StaReqPurpose purpose = new StaReqPurpose();
                purpose.setId(pur.getId());
                purpose.setBxFlowNo(pur.getBxWorkFlowNo());
                purpose.setBxCulCombinaNos(pur.getBxCulCombinaNos());
                upPurposeList.add(purpose);
            }
            bacReqPurposeService.updateBatchById(upPurposeList);
        }

        List<StaPurposeAndDetailDto> detailList = purposeAnddetailList.stream().filter(p -> p.getDataType().equals("detail")).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(detailList)) {
            List<StaReqPurposeDetail> upDetailList = new ArrayList<>();
            for (StaPurposeAndDetailDto del : detailList) {
                StaReqPurposeDetail detail = new StaReqPurposeDetail();
                detail.setId(del.getId());
                detail.setBxWorkFlowNo(del.getBxWorkFlowNo());
                //detail.setBxCultureCombinationIds(del.getBxCultureCombinationIds());
                detail.setBxCulCombinaNos(del.getBxCulCombinaNos());
                upDetailList.add(detail);
            }
            this.updateBatchById(upDetailList);
        }

        return Result.succ(1, "成功", null);
    }


}
